<!--
 - Author : wang885298.
 - Date   : 2018/6/15.
 - File   : share.分享组件
 -->

<template>
  <div class="share-content">
    <transition name="go">
      <div class="share" v-show="isShow">
        <ul class="share-list">
          <li :class="[item.className ,'share-item']" @click="isShow=false" v-for="(item, index) in shareList" :key="index"  >
            <div v-if="item.shareId==='copyurl'"
                v-clipboard:copy="message"
                v-clipboard:success="onCopy"
                v-clipboard:error="onError">
              <div class="share-list-img"
                  :style="'background:url('+item.imgUrl+') no-repeat center bottom;background-size:cover;'"></div>
              <p>{{item.name}}</p>
            </div>
            <div v-if="item.shareId!=='copyurl'">
              <div class="share-list-img"
                  :style="'background:url('+item.imgUrl+') no-repeat center bottom;background-size:cover;'"></div>
              <p>{{item.name}}</p>
            </div>
          </li>
        </ul>
        <div class="cancel-btn" @click="isShow = false">取消</div>
      </div>
    </transition>
    <div class="model" v-if="isShow" @click="isShow = false"></div>
  </div>
</template>
<script>
import {Toast} from 'mint-ui'
export default {
  data () {
    return {
      message: window.location.href,
      shareList: [
        {
          name: '微信',
          imgUrl: require('../assets/images/shareImg/weixin.png'),
          shareId: 'weixin',
          className: '-mob-share-weixin'
        },
        {
          name: '朋友圈',
          imgUrl: require('../assets/images/shareImg/pengyouquan.png'),
          shareId: 'pengyouquan',
          className: '-mob-share-weixin'
        },
        {
          name: '复制链接',
          imgUrl: require('../assets/images/shareImg/copyurl.png'),
          shareId: 'copyurl',
          className: ''
        },
        {
          name: '微博',
          imgUrl: require('../assets/images/shareImg/weibo.png'),
          shareId: 'weibo',
          className: '-mob-share-weibo'
        },
        {
          name: 'QQ好友',
          imgUrl: require('../assets/images/shareImg/qq.png'),
          shareId: 'qq',
          className: '-mob-share-qq'
        },
        {
          name: 'QQ空间',
          imgUrl: require('../assets/images/shareImg/qzone.png'),
          shareId: 'qzone',
          className: '-mob-share-qzone'
        }
      ],
      isShow: false
    }
  },
  mounted () {
    this.isShow = this.sheetVisible
  },
  methods: {
    onCopy (e) {
      console.log(e.text)
      Toast('复制成功！！！')
    },
    onError (e) {
      console.log(e.text)
    }
  },
  watch: {
    sheetVisible: {
      handler (newVal, oldVal) {
        this.isShow = newVal
        document.getElementsByClassName('share-list')[0].scrollLeft = 0
        this.message = window.location.href
      }
    },
    isShow: {
      handler (newVal, oldVal) {
        this.isShow = newVal
        this.$emit('change', newVal)
      }
    }
  },
  props: {
    sheetVisible: {type: Boolean, default: false}
  }
}
</script>

<style lang="less" scoped>
  /* 分享列表 */
  .share {
    position: fixed;
    background: #e0e0e0;
    width: 100%;
    text-align: center;
    left: 0;
    bottom: 0;
    background: #fff;
    z-index: 10000;
    .share-list {
      display: flex;
      flex-wrap: nowrap;
      overflow-x: auto;
      margin: 2rem 0 1rem;
      .share-item {
        .share-list-img {
          width: 5rem;
          height: 5rem;
          margin: 0 1.3rem;
        }
        p {
          text-align: center;
          margin: .5rem;
        }
      }
    }
    .share-list::-webkit-scrollbar {
      display: none;
    }
    .cancel-btn {
      height: 50px;
      line-height: 50px;
      border-top: 1px solid rgba(204, 204, 204, .5);
      text-align: center;
    }
  }

  /* 遮罩层 */
  .model {
    position: fixed;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    opacity: 0.5;
    background: #000;
    z-index: 2002;
  }

  /* 动画 */
  .go-enter-active, .go-leave-active {
    transition: all .2s;
    height: 179px;
  }

  .go-enter, .go-leave-to {
    height: 0;
  }
</style>
